package cc.openkit.admin.controller.api;

import cc.openkit.admin.model.User;
import cc.openkit.admin.service.user.UserService;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.google.gson.Gson;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 测试接口
 */
@Controller
@Scope("prototype")
@RequestMapping("/test")
public class TestController {

    private Logger log = Logger.getLogger(TestController.class);
    @Resource
    private  UserService userService;

    /**
     * 解析Json数组，并获取里面的数值
     * @return
     */
    @RequestMapping(value = "/test", method = RequestMethod.POST)
    @ResponseBody
    public void test(HttpServletRequest request){
        log.info("解析Json数组，并获取里面的数值");
        //获取json字符串数组
        String str = request.getParameter("str");
        JSONArray objects = JSONUtil.parseArray(str);
        for (int i = 0; i < objects.size(); i++) {
            cn.hutool.json.JSONObject obj = JSONUtil.parseObj(objects.get(i));
            String a = (String) obj.get("a");
            String b = (String) obj.get("b");
            System.out.println(a+"    "+b);
        }
    }

    @RequestMapping(value = "/test1", method = RequestMethod.POST)
    @ResponseBody
    public Object test1(HttpServletRequest request){
        String uid = request.getHeader("uid");
        User user = userService.queryByUUID(uid);
        System.out.println(user.getUserLoginName());
        return new Gson().toJson(user);
    }

}
